import { defineStore } from "pinia"
import mockRequest from "@/mock/request"
import initRouteListTable  from "./initRouteTable"
import {authBtnMode} from "@/common/config"

export const actionKeys={
  queryList:authBtnMode=='request'?'routeListQuery':'query',
  add:authBtnMode=='request'?'routeAdd':'add',
  update:authBtnMode=='request'?'routeUpdate':'update',
  delete:authBtnMode=='request'?'routeDelete':'delete',
}

export default defineStore("routeList", {
  state: () => ({
    list: [] as routeTreeNode[],
  }),
  actions: {
    async getList(params = {}) {
      const res = await mockRequest(()=>({
        code: 200,
        list: this.list.length ? this.list : initRouteListTable,
        
      }))
      if (res && res.code) {
        this.list = res.list
        return { code: 200, list: res.list }
      }
      return { code: 0 }
    },
      /** 
      @requestName routeListQuery  接口权限值
  **/
      async getPageList(params:{page:{page:number,size:number},cdt?:Partial<routeTreeNode>}){
        const res = await mockRequest(()=>{
          const {page:{page,size},cdt={}}=params
          let list:routeTreeNode[]=JSON.parse(JSON.stringify(this.list))
          Object.keys(cdt).forEach(k=>{
            if(k=='name') list=list.filter(v=>v.name.includes(cdt[k as string]) || cdt[k as string].includes(v.name))
            if(k=='component') list=list.filter(v=>(v.component&&v.component.includes(cdt[k])) || (v.component&&cdt[k].includes(v.component)))
            if(k=='pTitle') list=list.filter(v=>(v.pTitle&&v.pTitle.includes(cdt[k as string])) || (v.pTitle&&cdt[k as string].includes(v.pTitle)))
            if(k=='title') list=list.filter(v=>v.title.includes(cdt[k as string]) || cdt[k as string].includes(v.title))
            if(k=='noNeedAuth') list=list.filter(v=>Boolean(v.noNeedAuth)==Boolean(cdt[k]))
            if(k=='onInMenu') list=list.filter(v=>Boolean(v.onInMenu)==Boolean(cdt[k]))
            if(k=='noKeepAlive') list=list.filter(v=>Boolean(v.noKeepAlive)==Boolean(cdt[k]))
          })
          const total=list.length
          return { code: 200,message:'success',data:{total,list:list.splice((page-1)*size,size)},}
        })
       
        if (res && res.code) return { code: 200,message:res.message,data:res.data}
        return { code: 0 }
      },
  /** 
      @requestName routeDelete  接口权限值
  **/
    async deleteItem(params: { id: string | number }) {
      const res = await mockRequest(()=>{
        this.list.splice(this.list.findIndex((v) => v.id == params.id),1)
        return {code:200}
      },false)
      if (res && res.code) {
        return { code: 200 }
      }
      return { code: 0 }
    },
  /** 
      @requestName routeAdd  接口权限值
  **/
    async addItem(params: routeTreeNode) {
      const res = await mockRequest(()=>{
        if (!this.list.find((v) => v.name == params.name)) {
          this.list.unshift(params)
          return { code: 200 }
        } else return { code: 201 }
      })
      if (res && res.code == 200) {return { code: 200 }}
      if (res && res.code == 201) return { code: 201 ,msg:'已存在'}
      return { code: 0 }
    },
     /** 
      @requestName routeUpdate  接口权限值
  **/
    async updateItem(params: routeTreeNode) {
      const res = await mockRequest(()=>{
        if(!this.list.find(v=>v.id!=params.id && params.name==v.name))
        {
          const item=this.list.find(v=>v.id==params.id)
          Object.keys(params).forEach((k)=>{
            item![k]=params[k]
          })
          return { code: 200 }
        }
        else return {code:201}
      })
      if (res && res.code == 200) {return { code: 200 }}
      if (res && res.code == 201) return { code: 201,msg:'已存在'}
      return { code: 0 }
    },
  },
})
